const { search, insert, update, del, findOne } = require('../model/article.model');

class ArticleService {
  // 查询文章列表
  async searchArticle({ title, status, sort_id, pageNum, pageSize }) {
    const page = (pageNum-1) * pageSize;
    let whereStr = `where a.id <> '' and  b.id <> '' AND c.id <> ''`;
    let otherStr = ``
    // 左关联表
    let rightStr = `LEFT JOIN b_type b ON a.sort_id = b.id LEFT JOIN b_tag c ON a.tag_id = c.id`
    // orderby 
    let whereOrder = 'order by create_date desc';
    // 分页
    let whereLimit = `limit ${page}, ${pageSize}`;
    
    if(title) {
      whereStr += ` and title like '%${title}%'`
    }
    if(status) {
      whereStr += ` and status = '${status}'`
    }
    if(sort_id) {
      whereStr += ` and sort_id = '${sort_id}'`
    }

    const sql = `select a.id, a.title, a.content, a.create_date, a.status,b.id as sort_id, b.type_name,c.id as tag_id, c.tag_name from b_article a ${rightStr} ${whereStr} ${otherStr} ${whereOrder} ${whereLimit}`;
    const res = await search(sql)
    return res
  }

  // 查询标文章表总数
  async searchArticleTotal({ title, status, sort_id}) {
    // 右关联表
    let rightStr = `LEFT JOIN b_type b ON a.sort_id = b.id LEFT JOIN b_tag c ON a.tag_id = c.id`
    let whereStr = `where a.id <> '' and  b.id <> '' AND c.id <> ''`;
       
    // 查询条件
    if(title) {
      whereStr += ` and title like '%${title}%'`
    }
    if(status) {
      whereStr += ` and status = '${status}'`
    }
    if(sort_id) {
      whereStr += ` and sort_id = '${sort_id}'`
    }

    const sql = `select count(*) as total from b_article a ${rightStr} ${whereStr}`;
    const res = await search(sql)
    return res
  }

  // 添加文章
  async insertArticle({title, tag_id, sort_id, content, paths}) {
    const params = { title, tag_id, sort_id, content, paths }
    const res = await insert('b_article', params)
    return res
  }
  // 修改文章
  async updateArticle({id, title, tag_id, sort_id, content, path}) {
    const params = {}
    const whereOpt = {}

    id && Object.assign(whereOpt, { id })

    title && Object.assign(params, { title })
    content && Object.assign(params, { content })
    tag_id && Object.assign(params, { tag_id })
    sort_id && Object.assign(params, { sort_id })
    path && Object.assign(params, { paths: path })

    const res = await update('b_article', params, whereOpt)
    return res
  }
  // 删除文章
  async deleteArticle({id}) {
    const params = {}

    id && Object.assign(params, { id })
    const res = await del('b_article', params)
    return res
  }

  // 查询某一个文章
  async findArticle({id, title}) {
    const params = {}

    id && Object.assign(params, { id })
    title && Object.assign(params, { title })

    const res = await findOne('b_article', params)
    return res.length > 0 ? res : null;
  }
  // 发布文章
  async publishArticle({id, status}) {
    const params = {}
    const whereOpt = {}

    id && Object.assign(whereOpt, { id })

    status && Object.assign(params, { status })


    const res = await update('b_article', params, whereOpt)
    return res;
  }
}

module.exports = new ArticleService();
